package com.zhuiYun.project.util;
import com.zhuiYun.project.api.commodityType.entity.CommodityType;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {

        // 建立父节点树形结构
        public static List<CommodityType> buildTree(List<CommodityType> commodityType){
            ArrayList<CommodityType> sysDepts = new ArrayList<>();
            for (CommodityType commodityTypeEntity : getNodeTree(commodityType)) {
                getChildrenNode(commodityType, commodityTypeEntity);
                sysDepts.add(commodityTypeEntity);
            }
            return sysDepts;
        }
        // 查询所有的父节点，getParent()=0
        private static List<CommodityType> getNodeTree(List<CommodityType> commodityType){
            ArrayList<CommodityType> sysDepts = new ArrayList<>();
            for (CommodityType commodityTypeEntity : commodityType) {
                if (commodityTypeEntity.getTypeId()==0){
                    sysDepts.add(commodityTypeEntity);
                }
            }
            return sysDepts;
        }

        // 递归建立树形子节点，将子节点放到父节点下
        private static CommodityType getChildrenNode(List<CommodityType> commodityType,CommodityType commodityTypeEntity){
            ArrayList<CommodityType> commodityTypeEntitys = new ArrayList<>();
            for (CommodityType commodityTypeEntity1 : commodityType) {
                if (commodityTypeEntity1.getTypeId().equals(commodityTypeEntity.getId())){
                    commodityTypeEntitys.add(getChildrenNode(commodityType,commodityTypeEntity1));
                }
            }
            commodityTypeEntity.setChildren(commodityTypeEntitys);
            return commodityTypeEntity;
        }

}
